﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using UtilityExtensions;
using BOL;
using BOL.Ventas;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.Admin.Proyecto
{
    public partial class FolioEditGrid : System.Web.UI.UserControl
    {
        public FolioEditGrid()
        {
            bol = new FolioBL();
        }

        private FolioBL bol;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {

                idProyecto.Value = (string)Request.QueryString["id"];
                FormatForm();
            }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
            
        }
       
        
        private void FormatForm()
        {
                BindDataItem();
                setFooterRow();
        }


        protected void AddItem(object sender, EventArgs e)
        {
            try
            {

                 //int idEmpresa = (int)Session["idEmpresa"];
                
                string idDocumentoTipo =
                    ((DropDownList)itemGridView.FooterRow.FindControl("documentoTipoDDL")).SelectedValue;
                string serie =
                    ((TextBox)itemGridView.FooterRow.FindControl("serieTXT")).Text;
                string inicio =
                    ((TextBox)itemGridView.FooterRow.FindControl("inicioTXT")).Text;
                string fin =
                    ((TextBox)itemGridView.FooterRow.FindControl("finTXT")).Text;

                string actual =
                    ((TextBox)itemGridView.FooterRow.FindControl("actualTXT")).Text;

                bool bitActivo =
                     ((CheckBox)itemGridView.FooterRow.FindControl("bitActivoCHK")).Checked;


                bol.Add( idProyecto.Value.ToInt(), 
                        idDocumentoTipo.ToInt(), 
                        serie, 
                        inicio.ToInt(), 
                        fin.ToInt(),
                        actual.ToInt(),
                        bitActivo);

                BindDataItem();
                setFooterRow();
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());

                
                string serie =
                     ((TextBox)gv.Rows[e.RowIndex].FindControl("serieTXT")).Text;
                string inicio =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("inicioTXT")).Text;
                string fin =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("finTXT")).Text;

               

                bool bitActivo =
                     ((CheckBox)gv.Rows[e.RowIndex].FindControl("bitActivoCHK")).Checked;


                bol.Update(idinternal, inicio.ToInt(), fin.ToInt(), bitActivo);
                
                gv.EditIndex = -1;

                BindDataItem();
                setFooterRow();
                 GridUpdatePanel.Update();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = e.NewEditIndex;

                BindDataItem();
                itemGridView.FooterRow.Visible = false;
                GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = -1;
                BindDataItem();
                GridUpdatePanel.Update();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                var drw = DbUtil.Db.Folios.Single(p => p.idInternal == idInternal);

                //eliminamos de la coleccion
                DbUtil.Db.Folios.DeleteOnSubmit(drw);
                DbUtil.Db.SubmitChanges();
                BindDataItem();
                setFooterRow();
                GridUpdatePanel.Update();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty()
        {
            if (itemGridView.Rows.Count == 0)
            {
                List<FolioInfo> itemList0 = new List<FolioInfo>();
                FolioInfo itemcero = new FolioInfo();
                itemcero.idInternal = 0;
                itemList0.Add(itemcero);
                itemGridView.DataSource = itemList0;
                itemGridView.DataBind();
                itemGridView.Rows[0].Visible = false;
            }

        }

        public void BindDataItem()
        {


            itemGridView.DataSource = bol.GetFolio(idProyecto.Value.ToInt());
            itemGridView.DataBind();
            resetEmpty();

        }
        private void setFooterRow()
        {
            // debemos retirar aquillos que son ya configurados en el proyecto

            var actuales=bol.GetFolio(idProyecto.Value.ToInt()).Select(p=>p.idDocumentoTipo);
                
            var documentoventa = new ListValueBL().GetDocumentoTipoVenta().Where(p=>!actuales.Contains(p.idInternal));


            DropDownList obraInsertDropDownList = ((DropDownList)itemGridView.FooterRow.FindControl("documentoTipoDDL"));
            
            obraInsertDropDownList.DataValueField = "idInternal";
            obraInsertDropDownList.DataTextField = "descripcion";
            obraInsertDropDownList.DataSource = documentoventa;
            obraInsertDropDownList.DataBind();


        }

    }

}